# coding=utf-8
from selenium import webdriver
from bs4 import BeautifulSoup
from dao.pymysql_utils import pymysql_utils
import time

class war3_spider:

    def __init__(self):
        url = "https://www.douyu.com/search/?kw=%E9%AD%94%E5%85%BD%E4%BA%89%E9%9C%B8&label=live&type=popularity"
        self.driver = webdriver.PhantomJS()
        self.driver.get(url)
        time.sleep(1)
        self.message = ()
        self.pymysql_util = pymysql_utils("localhost", 3306, "root", "root","carry")
        # self.driver.save_screenshot("Y://war3.png")

    # 获取信息
    def get_message(self):
        html = self.driver.page_source      # 获取网页代码
        soup = BeautifulSoup(html)
        # 标签
        tags_tag = soup.select('span[class="tag ellipsis"]')  # 标签的标签
        tags = []
        for tag in tags_tag:
            tags.append(tag.get_text().encode("utf-8"))
        # 直播间名称
        names_tag = soup.select('h3[class="ellipsis"]')       # 直播间名的标签
        names = []
        for name in names_tag:
            names.append(name.get_text().encode("utf-8"))
        # 播主名称
        players_tag = soup.select('span[class="dy-name ellipsis fl"]')   # 播主名的标签
        players = []
        for player in players_tag:
            players.append(player.get_text().encode("utf-8"))
        # 热度
        hots_tag = soup.select('span[class="dy-num fr"]')    # 热度的标签
        hots = []
        for hot in hots_tag:
            hots.append(hot.get_text().encode("utf-8"))

        self.message = zip(tags, names, players, hots)    # 将所有数据打包

    # 保存数据到数据库
    def save_message(self):
        sql = "insert into douyu_war3(tag,name,player,hot) values(%s,%s,%s,%s)"
        for each in self.message:
            one_message = list(each)    # 元组转列表
            # print(one_message)
            self.pymysql_util.insert(sql,one_message)


if __name__ == "__main__":
    war3_spider = war3_spider()
    war3_spider.get_message()
    war3_spider.save_message()




